﻿<%@ WebHandler Language="VB" Class="GenericHandler1" %>

Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data

Public Class Handler : Implements IHttpHandler
    Dim conn As New SqlConnection("Data Source=sql.bluetonemedia.com;Initial Catalog=BlueToneMediaDB;Persist Security Info=True;User ID=BTMWiz;Password=esquel4ma@gic;Failover Partner=sqlfailover.bluetonemedia.com")
    Dim Qry As String
    Dim dv As New DataView
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim jsonstring As String
        
        jsonstring = "{query:'" & context.Request("query") & "',suggestions:["
        
        Qry = "SELECT TOP(50) * FROM  Contacts WHERE Company LIKE '%" & context.Request("query") & "%' order by id"
        conn.Open()
        Dim Sample As New DataSet
        Dim Sql As New SqlDataAdapter(Qry, conn)
        Sql.Fill(Sample)
        dv.Table = Sample.Tables(0)
        
        Dim i As Integer
        Dim lastname As String
        
        lastname = ""
        
        For i = 0 To Sample.Tables(0).Rows().Count - 1
            jsonstring = jsonstring + "'" & context.Server.HtmlDecode(dv(i)("FName")) & "',"
        Next i
        
        jsonstring = jsonstring.Remove(jsonstring.Length - 1, 1)
        
        jsonstring = jsonstring + "]}"
        context.Response.Write(jsonstring)
        
        'context.Response.ContentType = "text/plain"
        'context.Response.Write("Hello World")
        
        'Dim SQLString As String = "SELECT Count(FName) FROM Contacts WHERE FName LIKE '" & context.Request("query") & "%' or LName LIKE '" & context.Request("query") & "%'"

        'Dim cmd As New SqlCommand()
        'Dim recCount As Integer

        'conn.Open()

        'cmd.Connection = conn
        'cmd.CommandText = SQLString
        'recCount = Integer.Parse(cmd.ExecuteScalar().ToString)
        ''context.Response.Write(recCount)
        
        'conn.Close()
        
        'Qry = "SELECT TOP(50) * FROM  Contacts WHERE FName LIKE '" & context.Request("query") & "%' or LName LIKE '" & context.Request("query") & "%' order by id DESC"
        'conn.Open()
        'Dim Sample As New DataSet
        'Dim Sql As New SqlDataAdapter(Qry, conn)
        'Sql.Fill(Sample)
        'dv.Table = Sample.Tables(0)
        
        'Dim i As Integer
        'Dim lastname As String
        
        'lastname = ""
        
        'For i = 0 To Sample.Tables(0).Rows().Count - 1
        '    If IsDBNull(dv(i)("LName")) Then
        '        lastname = ""
        '    Else
        '        lastname = context.Server.HtmlDecode(dv(i)("LName"))
        '    End If
            
        '    jsonstring = jsonstring + "'" & context.Server.HtmlDecode(dv(i)("FName")) & " " & lastname & "',"
        'Next i
        
        'jsonstring = jsonstring.Remove(jsonstring.Length - 1, 1)
        
        'jsonstring = jsonstring + "]}"
        'context.Response.Write(jsonstring)
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class